package com.sc.server.system.batch.conf;


import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.sc.server.system.auth.entity.Menu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@EnableBatchProcessing
@Slf4j
public class StrBatchConfigiguration {
    Integer count = 0;
    @Autowired
    private JobListener jobListener;
    @Resource
    private JobBuilderFactory jobBuilderFactory;    //用于构建JOB
    @Resource
    private StepBuilderFactory stepBuilderFactory;  //用于构建Step
    @Bean
    public Job processStrJob() {
        return jobBuilderFactory.get("strJob")
                .incrementer(new RunIdIncrementer()).listener(jobListener)
                .flow(orderStep()).end().build();
    }
    @Bean
    public Step orderStep() {
        return  stepBuilderFactory.get("strStep").<String, String>chunk(100).
                //捕捉到异常就重试,重试100次还是异常,JOB就停止并标志失败
                        faultTolerant().retryLimit(3).retry(Exception.class).skipLimit(100).skip(Exception.class).
                        reader(getStrReader()).
                //指定ItemProcessor
                        processor(getStrProcess()).
                //指定ItemWriter
                        writer(getStrWriter()).
                        build();
    }

    @Bean
    public ItemReader<? extends String> getStrReader() {
         String[] messages = { "javainuse.com",
                "Welcome to Spring Batch Example",
                "We use H2 Database for this example" };

            return new ItemReader<String>() {
                @Override
                public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

                    if (count < messages.length) {
                        return messages[count++];
                    } else {
                        count = 0;
                    }
                    return null;
                }
            };
        }
        public ItemProcessor<String,String> getStrProcess() {
            return new ItemProcessor<String, String>() {
                @Override
                public String process(String s) throws Exception {
                    return s.toUpperCase();
                }
            };
        }
            public  ItemWriter<String> getStrWriter() {
                return new ItemWriter<String>() {
                    @Override
                    public void write(List<? extends String> list) throws Exception {
                        for (String s : list) {
                            System.out.println(s);
                        }
                    }
                };
            }
}
